package com.spring.aop1.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Author: lxh
 * @name: LogAspect02
 * Date: 2024/6/22  20:53
 */
@Component
@Aspect
@Order(1)
public class LogAspect02 {
    @Pointcut("execution(* com.spring.aop1.service.UserService.*(..))")
    public void GeneralPointcutExpression() {}

    // 前置通知
    @Before("GeneralPointcutExpression()")
    public void beforeAdvice() {
        System.out.println("优先级 1 前置通知...");
    }

    // 后置通知
    @AfterReturning("GeneralPointcutExpression()")
    public void afterReturningAdvice() {
        System.out.println("优先级 1 后置通知...");
    }

    /**
     * 环绕通知
     *
     * @param joinPoint 连接点
     */
    @Around("GeneralPointcutExpression()")
    public void aroundAdvice(ProceedingJoinPoint joinPoint) {
        // 前环绕
        System.out.println("优先级 1 前环绕...");
        // 调用执行目标
        try {
            joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        // 后环绕
        System.out.println("优先级 1 后环绕...");
    }

    // 最终通知
    @After("GeneralPointcutExpression()")
    public void afterAdvice() {
        System.out.println("优先级 1 最终通知...");
    }

    // 异常通知
    @AfterThrowing("GeneralPointcutExpression()")
    public void afterThrowingAdvice() {
        System.out.println("优先级 1 异常通知...");
    }
}
